package library.Thread;

/**
 * @author JNC
 */
import org.apache.log4j.*;

import library.RemoteControl.*;
import library.JpegNodes.*;

/**
 * The Class_ProcessingThread defines the thread structure to perform
 * the actions requested by the user on the jpeg files contained in the 
 * directory indicated by the user. <br/>
 * @author JNC
 */
public class ProcessingThread extends Thread {
    
    /**
     * The log4j logger
     */
    private Logger _logger = Logger.getLogger(ProcessingThread.class);
    
    /**
     * The starting directory.<br/>
     * Is a parameter of the thread.
     */
    private JpegDirectory _startingDirectory;
    
    /**
     * Indicator recalling if the user requested a recursive processing.<br/>
     * Is a parameter of the thread.
     */
    private boolean _isRecursive;
    
    /**
     * The action requested by the user.<br/>
     * Is a parameter of the thread.
     */
    private ActionTypeEnum _action;
    
    /**
     * A reference to the remote control.<br/>
     * Is a parameter of the thread.
     */
    private RemoteControlInterface _remoteControl;

    
    /**
     * Constructor
     * @param myStartingDirectory
     * The starting directory to apply the action
     * @param myAction
     * The action requested by the user
     * @param myIsRecursive
     * Indicator recalling if the users requested a recursive processing
     * @param myRemoteControl
     * Reference to the remote control, enabling the thread to request
     * interaction with the frame
     */
    public ProcessingThread(  String myStartingDirectory,
                                    ActionTypeEnum myAction, 
                                    boolean myIsRecursive,
                                    RemoteControlInterface myRemoteControl) 
    throws UnknownClassException{
        
        // sets the remote control attribute and register the thread in the
        // remotecontrol
        _remoteControl = myRemoteControl;
        _remoteControl.register(this);
        
        // sets the other attributes
        _isRecursive = myIsRecursive;
        _action = myAction;
        
        // the _remoteControl attribute is passed to the constructor of 
        // _startingDirectory in order to update the progress bar
        _startingDirectory = new JpegDirectory(myStartingDirectory,
                _remoteControl);
    }
    
    /**
     * Mandatory method when implementing a thread; <br/>
     * This method is called by the start() method.
     */
    public void run() {
        _logger.debug("The thread " + getName() + " starts...");
        
        // disables all the commands of the frame
        _logger.debug("Disables all the command of the frame");
        _remoteControl.enableCommands(false);
        
        // initializes the nbr of files to process and the number of files
        // already processed for the progress bar
        _startingDirectory.resetNbrFiles();
        
        // Starts processing all Jpeg files in the given directory
        // -------------------------------------------------------
        _startingDirectory.processJpegFiles(_action, _isRecursive);
        
        // enables again all the commands of the frame
        _logger.debug("Enables again all commands");
        _remoteControl.enableCommands(true);

        // sets the text of the GO/CANCEL button to GO (and not CANCEL any more)
        // also activates the GO/CANCEL button
        _logger.debug("Reinit the GO/CANCEL button to GO");
        _remoteControl.reinitGoCancelButton();
        
        // reinits the progress bar
        _remoteControl.reinitProgressBar();
        
        // unregisters the thread (the registration occured in the constructor)
        try {
            _remoteControl.unregister(this);
        }
        catch (UnknownClassException exception) {
            _logger.error("Exception_UnknownClassException caught while unregistering " + this.getClass() + " : " + exception );
        }
        
        _logger.info("End of processing");
        _logger.debug("The thread " + getName() + " stops...");
    }
    
    /**
     * Requests the processing thread to stop : the request to stop is passed
     * to the remote control --> Class_Processingthread --> Class_JpegDirectory
     */
    public void requestToStop() {
        _startingDirectory.requestToStop();
    }
}
